package net.sharpf2.api.util.web;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Retrieves data of the selected world.
 * @author Piotr Gajdowski
 */
public class WorldData {

    /**
     * Retrieves World Data of the specified world ID.
     * @param worldID       World number.
     * @param lang          Language (0=english, 1=german, 2=french, 3=portugues)
     * @return WorldData    
     */
    public static WorldData lookup(int worldID, int lang) {
        if (worldID > 0 || worldID < 176) {
            for (WorldData world : getData(lang)) {
                if (world.getWorldNumber() == worldID) { return world; }
            }
        }
        return null;
    }

    /**
     * Retrieves World Data of every world.
     * @param   lang        Language (0=english, 1=german, 2=french, 3=portugues)
     * @return  WorldData[]
     */
    public static WorldData[] getData(int lang) {
        try {
            int world = -1;
            int players = -1;
            boolean member = false;
            boolean lootshare = false;
            String activity = "";
            String location = "";
            URL url = new URL(Constants.languages[lang]);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String data;
            List<WorldData> worlds = new ArrayList<WorldData>();
            while ((data = br.readLine()) != null && !Thread.currentThread().isInterrupted()) {
                if (data.trim().matches("<td class=\"[fm]\">$")) {
                    member = data.contains("m");
                    data = br.readLine();
                    if (data.contains("<a href")) {
                        data = data.substring(data.indexOf(62)+1,data.length()-4);
                    }
                    data = data.substring(6);
                    world = Integer.parseInt(data);
                    br.readLine();
                    data = br.readLine();
                    if (data.trim().startsWith("</td>")) {
                        data = br.readLine();
                    }
                    data = data.substring(data.indexOf(62)+1,data.length()-5);
                    players = data.contains("(FULL|OFFLINE)") ? (data.equals("FULL") ? 2000 : -1) : Integer.parseInt(data);
                    data = br.readLine();
                    if (data.isEmpty()) {
                        data = br.readLine();
                    }
                    data = data.substring(data.indexOf(62)+1,data.length()-5);
                    if (data.contains("class\"a\"")) {
                        activity = data;
                    } else {
                        location = data;
                    }
                    br.readLine();
                    data = br.readLine();
                    if (data.isEmpty()) {
                        data = br.readLine();
                    }
                    data = data.substring(data.indexOf("title=\"")+7);
                    data = data.substring(0, data.indexOf(34));
                    lootshare = data.equals("Y") ? true : false;
                    worlds.add(new WorldData(world, players, activity, location, lootshare, member));
                }
            }
            br.close();
            return worlds.toArray(new WorldData[worlds.size()]);
        } catch (Exception ignored) {
            return new WorldData[0];
        }
    }

    private WorldData(int world, int players, String activity, String location, boolean lootShare, boolean member) {
        Constants.WORLD = world;
        Constants.PLAYERS = players;
        Constants.ACTIVITY = activity;
        Constants.LOCATION = location;
        Constants.LOOTSHARE = lootShare;
        Constants.MEMBER = member;
    }

    /**
     * Gets the Activity on the specified world.
     * @return <b>String</b> Activity
     */
    public String getActivity() {
        return Constants.ACTIVITY;
    }
    
    /**
     * Gets the Location on the specified world.
     * @return <b>String</b> Location
     */
    public String getLocation() {
        return Constants.LOCATION;
    }
    
    /**
     * Gets the Player amount on the specified world.
     * @return <b>int</b> Amount of players
     */
    public int getPlayers() {
        return Constants.PLAYERS;
    }
    
    /**
     * Gets the World Number on the specified world.
     * @return <b>int</b> World Number
     */
    public int getWorldNumber() {
        return Constants.WORLD;
    }
    
    /**
     * Checks if lootshare is allowed on the selected world.
     * @return true if lootshare is allowed, otherwise false.
     */
    public boolean isLootShare() {
        return Constants.LOOTSHARE;
    }
    
    /**
     * Checks if selected world is a members world or not.
     * @return true if world is for member, otherwise false.
     */
    public boolean isMember() {
        return Constants.MEMBER;
    }
    
    private static class Constants { 
        private static int WORLD;
        private static int PLAYERS;
        private static String LOCATION;
        private static String ACTIVITY;
        private static boolean LOOTSHARE;
        private static boolean MEMBER;
        private static String[] languages = {"http://www.runescape.com/slu.ws?order=WMLPA", 
            "http://www.runescape.com/l=1/slu.ws?order=WMLPA", 
            "http://www.runescape.com/l=2/slu.ws?order=WMLPA", 
            "http://www.runescape.com/l=3/slu.ws?order=WMLPA"};
    }
    
}